#ifndef DTWENTYITEM_H
#define DTWENTYITEM_H


#include "dtwenty_global.h"


#include <modelitem.h>


#include <QDebug>
#include <QSharedData>
#include <QSharedDataPointer>
#include <QPointer>


class DTWENTYSHARED_EXPORT DTwentyItem : public ModelItem
{
    Q_OBJECT

    class Data : public QSharedData
    {
    public:
        Data() {}
        Data(const Data& other) : QSharedData(other)
        {
            referencePath = other.referencePath;
        }

        QString referencePath;
        QPointer<DTwentyItem> referenceItem;
    };

    Q_PROPERTY(QString referencePath READ referencePath WRITE setReferencePath)

public:

    Q_INVOKABLE DTwentyItem(QObject* parent = 0);
    DTwentyItem(const DTwentyItem& other);

    virtual int type() const;
    virtual QString typeName() const;
    virtual bool removable() const;


    QString referencePath() const;
    void setReferencePath(const QString& referencePath);

    void setReferenceItem(DTwentyItem* referenceItem);

protected slots:

    void onResolveReference();
    void onRefreshReference();


protected:

    template<typename CHILD_CLASS>
    CHILD_CLASS* initChild(const QString& name)
    {
        CHILD_CLASS* childItem = findChild<CHILD_CLASS*>(QString(), Qt::FindDirectChildrenOnly);
        if (!childItem)
        {
            childItem = new CHILD_CLASS(this);
        }

        childItem->setName(name);
        return childItem;
    }

    const DTwentyItem* parentItem() const;


private:

    QSharedDataPointer<Data> d;

};
Q_DECLARE_METATYPE(DTwentyItem)


#endif // DTWENTYITEM_H
